home *** CD-ROM | disk | FTP | other *** search
- /*
- File: menus.c
-
- Copyright: © 1997 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #pragma segment AppSeg
-
- #ifndef __SCRAP__
- #include <Scrap.h>
- #endif
-
- #ifndef __DEVICES__
- #include <Devices.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
-
- #ifndef Common_Defs
- #include "Common.h"
- #endif
-
- extern Document* gDocumentList[kMaxDocumentCount];
- extern short gDocumentCount;
- extern short gCanUndoDrag;
- extern short gQuit, gQuitting;
- extern Boolean gNavServicesExists;
-
- // prototypes:
- void SetItemEnable(MenuHandle theMenu, short theItem, short enable);
- short DoPaste(void);
- void DoAbout(void);
- void DoSelectDictionary(void);
- void DoSelectDirectory(void);
- void DoSelectVolume(void);
- void DoCreateFolder(void);
-
-
- // *****************************************************************************
- // *
- // * SetItemEnable()
- // *
- // *****************************************************************************
- void SetItemEnable(MenuHandle theMenu, short theItem, short enable)
- {
- if (enable)
- EnableItem(theMenu,theItem);
- else
- DisableItem(theMenu,theItem);
- }
-
-
- // *****************************************************************************
- // *
- // * AdjustMenus()
- // *
- // *****************************************************************************
- void AdjustMenus()
- {
- MenuHandle theMenu;
- Document* theDocument;
- short teSelection = 0;
- Str255 theStr;
-
- theDocument = IsDocumentWindow((WindowPtr)FrontWindow());
-
- if (theDocument->theTE != NULL)
- teSelection = (theDocument) && ((**(theDocument->theTE)).selStart != (**(theDocument->theTE)).selEnd);
-
- theMenu = GetMenuHandle(idFileMenu);
-
- SetItemEnable(theMenu,NewItem,gDocumentCount < kMaxDocumentCount);
- SetItemEnable(theMenu,OpenItem,gDocumentCount < kMaxDocumentCount);
-
- SetItemEnable(theMenu,CloseItem,theDocument != 0L);
- SetItemEnable(theMenu,SaveItem,(theDocument) && (theDocument->dirty));
- SetItemEnable(theMenu,RevertItem,(theDocument) && (theDocument->dirty) && (theDocument->fRefNum));
- SetItemEnable(theMenu,SaveACopyItem,theDocument != 0L);
- SetItemEnable(theMenu,DictionaryItem,gNavServicesExists);
-
- theMenu = GetMenuHandle(idEditMenu);
-
- GetIndString(theStr,MenuStringsID,gCanUndoDrag);
- SetMenuItemText(theMenu,iUndo,theStr);
- SetItemEnable(theMenu,iUndo,gCanUndoDrag != slCantUndo);
-
- SetItemEnable(theMenu,iCut,teSelection);
- SetItemEnable(theMenu,iCopy,teSelection);
- SetItemEnable(theMenu,iPaste,theDocument != 0L);
- SetItemEnable(theMenu,iClear,teSelection);
- SetItemEnable(theMenu,iSelectAll,theDocument != 0L);
-
- theMenu = GetMenuHandle(idUtilsMenu);
- SetItemEnable(theMenu,iSelectDir,gNavServicesExists);
- SetItemEnable(theMenu,iSelectVol,gNavServicesExists);
- SetItemEnable(theMenu,iCreateFolder,gNavServicesExists);
- }
-
-
- // *****************************************************************************
- // *
- // * DoPaste()
- // *
- // *****************************************************************************
- short DoPaste()
- {
- long size, offset;
- Handle theData;
-
- size = GetScrap(0L,'UPRC',&offset);
-
- if (size <= 0)
- return(0);
-
- theData = NewHandle(size);
- GetScrap(theData,'UPRC',&offset);
-
- HLock(theData);
- PutScrap(size,'test',*theData);
-
- HUnlock(theData);
- DisposeHandle(theData);
-
- return noErr;
- }
-
-
- // *****************************************************************************
- // *
- // * DoSelectDictionary()
- // *
- // *****************************************************************************
- void DoSelectDictionary()
- {
- const OSType fileType = 'DICT'; // the file type to be chosen
-
- NavReplyRecord theReply;
- NavDialogOptions dialogOptions;
- OSErr theErr = noErr;
- NavEventProcUPP eventProcUPP = NewNavEventProc(myEventProc);
-
- // use the default location for the dialog:
- theErr = NavGetDefaultDialogOptions(&dialogOptions);
-
- //•• set the message field to 'message', to be define in .h file someday, put in STR#
- Str255 message = "\pPlease find the dictionary file:";
- BlockMoveData(message+1,dialogOptions.message+1,message[0]);
- dialogOptions.message[0] = message[0];
-
- dialogOptions.allowPreviews = false;
- dialogOptions.preferenceKey = kSelectFilePrefKey;
-
- theErr = NavChooseFile( NULL,
- &theReply,
- &dialogOptions,
- eventProcUPP,
- NULL,
- (NavCallBackUserData)&gDocumentList,
- fileType);
-
- DisposeRoutineDescriptor(eventProcUPP);
-
- if ((theReply.validRecord)&&(theErr == noErr))
- {
- // grab the target FSSpec from the AEDesc:
- FSSpec finalFSSpec;
- AEDesc resultDesc;
-
- theErr = AECoerceDesc(&(theReply.selection),typeFSS,&resultDesc);
- if (theErr == noErr)
- {
- BlockMove(*resultDesc.dataHandle,(void*)&finalFSSpec,sizeof(FSSpec));
- // 'finalFSSpec' is the chosen file…
- }
-
- theErr = NavDisposeReply(&theReply);
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DoSelectDirectory()
- // *
- // *****************************************************************************
- void DoSelectDirectory()
- {
- NavReplyRecord theReply;
- NavDialogOptions dialogOptions;
- OSErr theErr = noErr;
- NavEventProcUPP eventProcUPP = NewNavEventProc(myEventProc);
-
- theErr = NavGetDefaultDialogOptions(&dialogOptions);
-
- //•• set the message field to 'message', to be define in .h file someday, put in STR#
- Str255 message = "\pPlease select a directory:";
- BlockMoveData(message+1,dialogOptions.message+1,message[0]);
- dialogOptions.message[0] = message[0];
-
- dialogOptions.preferenceKey = kSelectFolderPrefKey;
-
- theErr = NavChooseFolder( NULL,
- &theReply,
- &dialogOptions,
- eventProcUPP,
- (NavCallBackUserData)&gDocumentList);
-
- DisposeRoutineDescriptor(eventProcUPP);
-
- if ((theReply.validRecord)&&(theErr == noErr))
- {
- // grab the target FSSpec from the AEDesc:
- FSSpec finalFSSpec;
- AEDesc resultDesc;
-
- theErr = AECoerceDesc(&(theReply.selection),typeFSS,&resultDesc);
- if (theErr == noErr)
- {
- BlockMove(*resultDesc.dataHandle,(void*)&finalFSSpec,sizeof(FSSpec));
- // 'finalFSSpec' is the selected directory…
- }
-
- theErr = NavDisposeReply(&theReply);
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DoSelectVolume()
- // *
- // *****************************************************************************
- void DoSelectVolume()
- {
- NavReplyRecord theReply;
- NavDialogOptions dialogOptions;
- OSErr theErr = noErr;
- NavEventProcUPP eventProcUPP = NewNavEventProc(myEventProc);
-
- // use the default location for the dialog:
- theErr = NavGetDefaultDialogOptions(&dialogOptions);
-
- //•• set the message field to 'message', to be define in .h file someday, put in STR#
- Str255 message = "\pPlease select a volume:";
- BlockMoveData(message+1,dialogOptions.message+1,message[0]);
- dialogOptions.message[0] = message[0];
-
- dialogOptions.preferenceKey = kSelectVolumePrefKey;
-
- theErr = NavChooseVolume(&theReply,
- &dialogOptions,
- eventProcUPP,
- (NavCallBackUserData)&gDocumentList);
- if ((theReply.validRecord)&&(theErr == noErr))
- {
- // grab the target FSSpec from the AEDesc:
- FSSpec finalFSSpec;
- AEDesc resultDesc;
-
- // there is only one selection here we get only the first AEDesc:
- theErr = AECoerceDesc(&(theReply.selection),typeFSS,&resultDesc);
- if (theErr == noErr)
- {
- BlockMove(*resultDesc.dataHandle,(void*)&finalFSSpec,sizeof(FSSpec));
- // 'finalFSSpec' is the chosen volume…
- }
-
- theErr = NavDisposeReply(&theReply);
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DoCreateFolder()
- // *
- // *****************************************************************************
- void DoCreateFolder()
- {
- NavReplyRecord theReply;
- NavDialogOptions dialogOptions;
- OSErr theErr = noErr;
- NavEventProcUPP eventProcUPP = NewNavEventProc(myEventProc);
-
- // use the default location for the dialog:
- theErr = NavGetDefaultDialogOptions(&dialogOptions);
-
- //•• set the message field to 'message', to be define in .h file someday, put in STR#
- Str255 message = "\pPlease create a folder:";
- BlockMoveData(message+1,dialogOptions.message+1,message[0]);
- dialogOptions.message[0] = message[0];
-
- dialogOptions.preferenceKey = kNewFolderPrefKey;
-
- theErr = NavNewFolder( NULL,
- &theReply,
- &dialogOptions,
- eventProcUPP,
- (NavCallBackUserData)&gDocumentList);
- if (theReply.validRecord)
- {
- // grab the target FSSpec from the AEDesc:
- FSSpec finalFSSpec;
- AEDesc resultDesc;
-
- // there is only one selection here so we get the first AEDesc:
- theErr = AECoerceDesc(&(theReply.selection),typeFSS,&resultDesc);
- if (theErr == noErr)
- {
- BlockMove(*resultDesc.dataHandle,(void*)&finalFSSpec,sizeof(FSSpec));
- // 'finalFSSpec' is the newly created folder…
- }
-
- theErr = NavDisposeReply(&theReply);
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DoAbout()
- // *
- // *****************************************************************************
- void DoAbout()
- {
- GrafPtr savePort;
- DialogPtr aboutDialog;
- short itemHit = 0;
- Handle itemH;
- short itemType;
- Rect itemRect;
-
- SetCursor(&qd.arrow);
- aboutDialog = GetNewDialog(rAboutID,nil,(WindowRef)-1);
-
- GetPort(&savePort);
-
- SetPort((GrafPtr)aboutDialog);
-
- AdornButton(aboutDialog,dOK);
-
- GetDialogItem(aboutDialog,iIconSuite,&itemType,&itemH,&itemRect);
- DrawIconSuite(rIconSuite,itemRect);
-
- do ModalDialog(nil,&itemHit);
-
- while(itemHit != dOK);
-
- SetPort(savePort);
- DisposeDialog(aboutDialog);
- }
-
-
- // *****************************************************************************
- // *
- // * DoMenuCommand()
- // *
- // *****************************************************************************
- void DoMenuCommand(long select)
- {
- short theMenuID, theItem;
- MenuHandle theMenu;
- Str255 theName;
- WindowPtr theWindow;
- Document* theDocument = nil;
- OSStatus theErr = noErr;
-
- theDocument = IsDocumentWindow(theWindow = (WindowPtr)FrontWindow());
-
- theItem = LoWord(select);
- theMenuID = HiWord(select);
- theMenu = GetMenuHandle(theMenuID);
- switch(theMenuID)
- {
- case idAppleMenu:
- switch(theItem)
- {
- case AboutItem:
- DoAbout();
- break;
- default:
- GetMenuItemText(GetMenuHandle(idAppleMenu),theItem,(unsigned char*)&theName);
- OpenDeskAcc(theName);
- }
- break;
-
- case idFileMenu:
- switch(theItem)
- {
- case NewItem:
- DoNewDocument(false);
- AdjustMenus();
- break;
-
- case OpenItem:
- if (gNavServicesExists)
- (void)DoOpenDocument();
- else
- (void)DoOpenDocumentTheOldWay();
- AdjustMenus();
- break;
-
- case CloseItem:
- if (theDocument)
- {
- CloseDocument(theDocument,false);
- AdjustMenus();
- DrawMenuBar();
- }
- break;
-
- case SaveItem:
- if (theDocument)
- DoSaveDocument(theDocument);
- break;
-
- case SaveACopyItem:
- if (theDocument)
- {
- if (gNavServicesExists)
- (void)SaveACopyDocument(theDocument);
- else
- (void)SaveACopyDocumentTheOldWay(theDocument);
- }
- break;
-
- case RevertItem:
- DisableUndoDrag();
- if (theDocument)
- {
- if (gNavServicesExists)
- DoRevertDocument(theDocument);
- else
- DoRevertDocumentTheOldWay(theDocument);
- }
- break;
-
- case DictionaryItem:
- DoSelectDictionary();
- break;
-
- case QuitItem:
- gQuitting = true;
- while ((gQuitting) && (theDocument = IsDocumentWindow((WindowPtr)FrontWindow())))
- CloseDocument(theDocument,true);
- if (gQuitting)
- gQuit = true;
- break;
- }
- break;
-
- case idEditMenu:
- switch(theItem)
- {
- case iUndo:
- DoUndoDrag();
- break;
-
- case iCut:
- if ((theDocument)&&(theDocument->theTE != NULL))
- {
- DisableUndoDrag();
- myTECut(theDocument->theTE);
- }
- break;
-
- case iCopy:
- if (theDocument)
- TECopy(theDocument->theTE);
- break;
-
- case iPaste:
- if ((theDocument)&&(theDocument->theTE != NULL))
- if (!DoPaste())
- {
- DisableUndoDrag();
- myTEPaste(theDocument->theTE,0L,0L);
- }
- break;
-
- case iClear:
- if ((theDocument)&&(theDocument->theTE != NULL))
- {
- DisableUndoDrag();
- TEDelete(theDocument->theTE);
- }
- break;
-
- case iSelectAll:
- if ((theDocument)&&(theDocument->theTE != NULL))
- DoSelectAllDocument(theDocument);
- break;
- }
-
- case idUtilsMenu:
- switch (theItem)
- {
- case iSelectDir:
- DoSelectDirectory();
- break;
- case iSelectVol:
- DoSelectVolume();
- break;
- case iCreateFolder:
- DoCreateFolder();
- break;
- }
- }
-
- if (theDocument = IsDocumentWindow((WindowPtr)FrontWindow()))
- if (theDocument->theTE != NULL)
- TEGetHiliteRgn(theDocument->hiliteRgn,theDocument->theTE);
-
- HiliteMenu(0);
- }
-
-